<template>
  <Drawer
      title="分组信息编辑"
      v-model="visible"
      width="450"
      :mask-closable="false"
      :styles="$PnUtil.buildDrawerStyleByHasFooter()">
    <Form ref="form" :label-width="80" :model="formData" :rules="ruleValidate">
      <FormItem label="id" prop="id" hidden>
        <Input v-model="formData.id"/>
      </FormItem>
      <FormItem label="分组名称" prop="group_name">
        <Input v-model="formData.group_name" placeholder="请输入分组名称"/>
      </FormItem>
      <FormItem label="排序" prop="sort">
        <InputNumber :max="1000" :min="1" v-model="formData.sort"></InputNumber>
      </FormItem>
      <FormItem label="备注" prop="remark">
        <Input type="textarea" :rows="4" v-model="formData.remark"/>
      </FormItem>
    </Form>
    <DrawerFooter>
      <Button class="m-r-5px" @click="visible = false">关闭</Button>
      <Button type="primary" @click="submitForm" :loading="loading">保存</Button>
    </DrawerFooter>
  </Drawer>
</template>

<script>
import ManageMixin from "@/mixin/ManageMixin";

export default {
  name: 'EnshrineCompGroupForm',
  mixins: [ManageMixin],
  data() {
    return {
      visible: false,
      loading: false,
      formData: {
        id: '',
        group_name: '',
        sort: 1,
        remark: ''
      },
      ruleValidate: {
        group_name: [
          {required: true, message: '分组名称不能为空', trigger: 'blur'}
        ]
      }
    }
  },
  mounted() {

  },
  methods: {
    init(type, id) {
      this.visible = true
      this.$nextTick(() => {
        this.$refs.form.resetFields()
        if (type == 'edit') {
          this.$PnApi.EnshrineCompApi.getEnshrineCompGroupById(id).then(result => {
            this.formData = this.recover(this.formData, result.data.payload)
          })
        }
      })
    },
    submitForm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.loading = true
          this.$PnApi.EnshrineCompApi.saveEnshrineCompGroup(this.formData).then(result => {
            if (result.data.success) {
              this.visible = false
              this.$Message.success(result.data.message)
              this.$emit("refreshDataList")
            }else {
              this.$Message.error(result.data.message)
            }
            this.loading = false
          })
        }
      });
    }
  },
  computed: {}
}
</script>

<style scoped>

</style>
